<?php
require_once 'application/controllers/Administrador.php';

class MainView extends CI_Controller{
    
    function __construct() {
        parent::__construct();
        $this->load->model('ExamModel');
        
    }
    
    function index() {
        if ($this->ExamModel->areSpecialExams()) {
            $this->load->view('LoginCueView');
        }else{
            $this->load->view('LoginView');
        }
    }
    
    public function checkLogin() {
        $this->session->set_userdata('userCue',null);
        $usersMatchArray = $this->verifyUser();
        
        switch (count($usersMatchArray)){
        case 0:
            $this->load->view('LoginView');
            break;
        case 1:
            $userInfo = $usersMatchArray[0];
            
            if ($userInfo->userTypeId == 1) {
                $this->session->set_userdata('userInformation',$userInfo);
                redirect('Administrador');
                
            }else if ($userInfo->userTypeId == 2) {
                $this->session->set_userdata('userInformation',$userInfo);
                redirect('Profesor');
                
            }else if ($userInfo->userTypeId == 3) {
                $this->session->set_userdata('userInformation',$userInfo);
                redirect('Alumno');
            }
            break;
        default:
            break;
        }
    }
    
    public function submitLoginCue() {
        $userCue = $this->input->post('userCue');
        $this->session->set_userdata('userCue',$userCue);
        $data['courses'] = $this->ExamModel->getExamsByDate();
        $this->load->view('CourseSelectView',$data);
    }
    
    public function submitCourseSelection(){
        $courseSelected = $this->input->post('courseSelectInput');
        $this->session->set_userdata('courseSelected',$courseSelected);
        redirect('Alumno');
    }
    
    public function verifyUser() {
        $userName = $this->input->post('userName');
        $password = $this->input->post('password');
        $this->load->model('UserModel');
        $userInfo = $this->UserModel->loginUser($userName, $password);
        return $userInfo;
    }
}
